module LCD_CTRL(clk, reset, IROM_Q, cmd, cmd_valid, IROM_EN, IROM_A, IRB_RW, IRB_D, IRB_A, busy, done);
input clk;
input reset;
input [7:0] IROM_Q;
input [2:0] cmd;
input cmd_valid;
output IROM_EN;
output [5:0] IROM_A;
output IRB_RW;
output [7:0] IRB_D;
output [5:0] IRB_A;
output busy;
output done;

reg IROM_EN;
reg [5:0] IROM_A;
reg IRB_RW;
reg [7:0] IRB_D;
reg [5:0] IRB_A;
reg busy;
reg done;
//reg [2:0] count_out;
reg [7:0] image [63:0];
reg [2:0] oper_x;//operation_point +1~+7
reg [2:0] oper_y;//operation_point +1~+7
reg [5:0] coor_0;
reg [5:0] coor_1;
reg [5:0] coor_2;
reg [5:0] coor_3;
reg pos_clk;

wire [3:0] temp_cmd;

/*wire [7:0]  image_00;*/
//wire [7:0]  image_01;
//wire [7:0]  image_02;
//wire [7:0]  image_03;
//wire [7:0]  image_04;
//wire [7:0]  image_05;
//wire [7:0]  image_06;
//wire [7:0]  image_07 ;
//wire [7:0]  image_08 ;
//wire [7:0]  image_09 ;
//wire [7:0]  image_10 ;
//wire [7:0]  image_11 ;
//wire [7:0]  image_12 ;
//wire [7:0]  image_13 ;
//wire [7:0]  image_14 ;
//wire [7:0]  image_15 ;
//wire [7:0]  image_16 ;
//wire [7:0]  image_17 ;
//wire [7:0]  image_18 ;
//wire [7:0]  image_19 ;
//wire [7:0]  image_20 ;
//wire [7:0]  image_21 ;
//wire [7:0]  image_22 ;
//wire [7:0]  image_23 ;
//wire [7:0]  image_24 ;
//wire [7:0]  image_25 ;
//wire [7:0]  image_26 ;
//wire [7:0]  image_27 ;
//wire [7:0]  image_28 ;
//wire [7:0]  image_29 ;
//wire [7:0]  image_30 ;
//wire [7:0]  image_31 ;
//wire [7:0]  image_32 ;
//wire [7:0]  image_33 ;
//wire [7:0]  image_34 ;
//wire [7:0]  image_35 ;
//wire [7:0]  image_36 ;
//wire [7:0]  image_37 ;
//wire [7:0]  image_38 ;
//wire [7:0]  image_39 ;
//wire [7:0]  image_40 ;
//wire [7:0]  image_41 ;
//wire [7:0]  image_42 ;
//wire [7:0]  image_43 ;
//wire [7:0]  image_44 ;
//wire [7:0]  image_45 ;
//wire [7:0]  image_46 ;
//wire [7:0]  image_47 ;
//wire [7:0]  image_48 ;
//wire [7:0]  image_49 ;
//wire [7:0]  image_50 ;
//wire [7:0]  image_51 ;
//wire [7:0]  image_52 ;
//wire [7:0]  image_53 ;
//wire [7:0]  image_54 ;
//wire [7:0]  image_55 ;
//wire [7:0]  image_56 ;
//wire [7:0]  image_57 ;
//wire [7:0]  image_58 ;
//wire [7:0]  image_59 ;
//wire [7:0]  image_60 ;
//wire [7:0]  image_61 ;
//wire [7:0]  image_62 ;
/*wire [7:0]  image_63 ;*/

assign temp_cmd = (cmd_valid)?{1'b0,cmd}:4'b1111;


//assign image_00 = image[0];
//assign image_01 = image[1];
//assign image_02 = image[2];
//assign image_03 = image[3];
//assign image_04 = image[4];
//assign image_05 = image[5];
//assign image_07 = image[7];
//assign image_08 = image[8];
//assign image_09 = image[9];
//assign image_10 = image[10];
//assign image_11 = image[11];
//assign image_12 = image[12];
//assign image_13 = image[13];
//assign image_14 = image[14];
//assign image_15 = image[15];
//assign image_16 = image[16];
//assign image_17 = image[17];
//assign image_18 = image[18];
//assign image_19 = image[19];
//assign image_20 = image[20];
//assign image_21 = image[21];
//assign image_22 = image[22];
//assign image_23 = image[23];
//assign image_24 = image[24];
//assign image_25 = image[25];
//assign image_26 = image[26];
//assign image_27 = image[27];
//assign image_28 = image[28];
//assign image_29 = image[29];
//assign image_30 = image[30];
//assign image_31 = image[31];
//assign image_32 = image[32];
//assign image_33 = image[33];
//assign image_34 = image[34];
//assign image_35 = image[35];
//assign image_36 = image[36];
//assign image_37 = image[37];
//assign image_38 = image[38];
//assign image_39 = image[39];
//assign image_40 = image[40];
//assign image_41 = image[41];
//assign image_42 = image[42];
//assign image_43 = image[43];
//assign image_44 = image[44];
//assign image_45 = image[45];
//assign image_46 = image[46];
//assign image_47 = image[47];
//assign image_48 = image[48];
//assign image_49 = image[49];
//assign image_50 = image[50];
//assign image_51 = image[51];
//assign image_52 = image[52];
//assign image_53 = image[53];
//assign image_54 = image[54];
//assign image_55 = image[55];
//assign image_56 = image[56];
//assign image_57 = image[57];
//assign image_58 = image[58];
//assign image_59 = image[59];
//assign image_60 = image[60];
//assign image_61 = image[61];
//assign image_62 = image[62];
//assign image_63 = image[63];

//pos_clk
always@(clk)
  pos_clk<=~clk;

always@(negedge clk) 
  begin
/*    if( reset )begin*/
      //image[	0	]<=0;
      //image[	1	]<=0;
      //image[	2	]<=0;
      //image[	3	]<=0;
      //image[	4	]<=0;
      //image[	5	]<=0;
      //image[	6	]<=0;
      //image[	7	]<=0;
      //image[	8	]<=0;
      //image[	9	]<=0;
      //image[	10	]<=0;
      //image[	11	]<=0;
      //image[	12	]<=0;
      //image[	13	]<=0;
      //image[	14	]<=0;
      //image[	15	]<=0;
      //image[	16	]<=0;
      //image[	17	]<=0;
      //image[	18	]<=0;
      //image[	19	]<=0;
      //image[	20	]<=0;
      //image[	21	]<=0;
      //image[	22	]<=0;
      //image[	23	]<=0;
      //image[	24	]<=0;
      //image[	25	]<=0;
      //image[	26	]<=0;
      //image[	27	]<=0;
      //image[	28	]<=0;
      //image[	29	]<=0;
      //image[	30	]<=0;
      //image[	31	]<=0;
      //image[	32	]<=0;
      //image[	33	]<=0;
      //image[	34	]<=0;
      //image[	35	]<=0;
      //image[	36	]<=0;
      //image[	37	]<=0;
      //image[	38	]<=0;
      //image[	39	]<=0;
      //image[	40	]<=0;
      //image[	41	]<=0;
      //image[	42	]<=0;
      //image[	43	]<=0;
      //image[	44	]<=0;
      //image[	45	]<=0;
      //image[	46	]<=0;
      //image[	47	]<=0;
      //image[	48	]<=0;
      //image[	49	]<=0;
      //image[	50	]<=0;
      //image[	51	]<=0;
      //image[	52	]<=0;
      //image[	53	]<=0;
      //image[	54	]<=0;
      //image[	55	]<=0;
      //image[	56	]<=0;
      //image[	57	]<=0;
      //image[	58	]<=0;
      //image[	59	]<=0;
      //image[	60	]<=0;
      //image[	61	]<=0;
      //image[	62	]<=0;
      //image[	63	]<=0;
    /*end*/
    //else if(cmd ==0)
    if(~IROM_EN) 
      image[IROM_A]<= IROM_Q;
    else if(temp_cmd ==5)begin
      image[coor_0] <= (image[coor_0] + image[coor_1] +image[coor_2] + image[coor_3]) / 4;
      image[coor_1] <= (image[coor_0] + image[coor_1] +image[coor_2] + image[coor_3]) / 4;
      image[coor_2] <= (image[coor_0] + image[coor_1] +image[coor_2] + image[coor_3]) / 4;
      image[coor_3] <= (image[coor_0] + image[coor_1] +image[coor_2] + image[coor_3]) / 4;
    end
    else if(temp_cmd ==6)begin
      image[coor_0] <= image[coor_2];
      image[coor_1] <= image[coor_3];
      image[coor_2] <= image[coor_0];
      image[coor_3] <= image[coor_1];
    end
    else if(temp_cmd ==7)begin
      image[coor_0] <= image[coor_1];
      image[coor_1] <= image[coor_0];
      image[coor_2] <= image[coor_3];
      image[coor_3] <= image[coor_2];
    end
    //else if(~IROM_EN) 
      //image[IROM_A]<= IROM_Q;
  end



//4_xy coor_0,coor_1,coor_2,coor_3
//cadence sync_set_reset "reset"
always@(oper_x or oper_y or reset)
  begin
    if( reset )begin
      coor_0 = 0;
      coor_1 = 0;
      coor_2 = 0;
      coor_3 = 0;
    end
    else begin
      coor_0 = ( (oper_y - 1'b1) << 3 ) + (oper_x - 1'b1) ; //(y-1) * 8 + x - 1
      coor_1 = ( (oper_y - 1'b1) << 3 ) + (oper_x) ; //(y-1) * 8 + x 
      coor_2 = ( oper_y  << 3 ) + (oper_x - 1'b1) ; //y * 8 + (x - 1)
      coor_3 = ( oper_y  << 3 ) + (oper_x) ; //y* 8 +x
    end
  end



//oper_x
//cadence sync_set_reset "reset"
always@( negedge clk )
  begin
    if( reset )
      oper_x <= 4;
    else if ( temp_cmd == 3 && oper_x > 1'b1)
      oper_x <= oper_x - 1'b1;
    else if ( temp_cmd == 4 && oper_x < 7)
      oper_x <= oper_x + 1'b1;
  end
//oper_y
//cadence sync_set_reset "reset"
always@( negedge clk )
  begin
    if( reset )
      oper_y <= 4;
    else if ( temp_cmd == 1 && oper_y> 1)
      oper_y <= oper_y - 1'b1;
    else if ( temp_cmd == 2 && oper_y< 7)
      oper_y <= oper_y + 1'b1;
  end
 


//IROM_EN
//cadence sync_set_reset "reset"
always@( negedge clk )
  begin
    if( reset )
      IROM_EN <= 0;
    else if ( IROM_A == 63 ) 
      IROM_EN <= 1;
  end

//busy
//cadence sync_set_reset "reset"
always@( negedge pos_clk )
  begin
    if( reset )
      busy <= 1;
    else if (temp_cmd == 0)
      busy <= 1;
    else if ( IROM_A == 62)
      busy <= 0;
  end
//IROM_A
//cadence sync_set_reset "reset"
always@( negedge clk )
  begin
    if( reset )
      IROM_A <= 0;
    else if (IROM_A == 63)
      IROM_A <= IROM_A;
    else 
      IROM_A <= IROM_A + 1'b1;
  end

//done                                  
//cadence sync_set_reset "reset"
always@( negedge clk )
  begin
    if( reset )
      done <= 0;
    else if ( IRB_A == 63 )
      done <= 1;
  end
//IRB_RW
//cadence sync_set_reset "reset"
always@( negedge clk )
  begin
    if( reset )
      IRB_RW <= 1'b1;
    else if ( IRB_A == 63)
      IRB_RW <= 1'b1;
    else if ( temp_cmd==0  )
      IRB_RW <= 0;
  end
//IRB_A
//cadence sync_set_reset "reset"
always@( negedge clk )
  begin
    if (reset)
      IRB_A <= 0; 
    else if(~IRB_RW)
      IRB_A <= IRB_A + 1'b1;
  end
//IRB_D
//cadence sync_set_reset "reset"
always@( negedge clk )
  begin
    if(reset )
      IRB_D <= 0;
    else if (~IRB_RW)
      IRB_D <= image[IRB_A+1'b1];
    else 
      IRB_D <= image[0];
  end

endmodule

